package com.cn.GenerateJson;

// 文件名：JsonFileExample.java
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

public class JsonFileExample {
    public static void main(String[] args) {
        // 创建对象
        Person person = new Person("Alice", 30);

        // 创建 ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        // 定义 JSON 文件路径
        File file = new File("person.json");

        try {
            // 将对象写入 JSON 文件
            mapper.writeValue(file, person);
            System.out.println("写入成功: " + file.getAbsolutePath());

            // 从 JSON 文件读取对象
            Person readPerson = mapper.readValue(file, Person.class);
            System.out.println("读取成功: " + readPerson);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
